//
//  ContactsViewController.m
//  iTinyBag
//
//  Created by maxh on 12/19/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ContactsViewController.h"

#define BAR_COLOR	[UIColor grayColor]
#define SELECTION_COLOR	[UIColor lightGrayColor]
#define Backup_Url @"http://demo4me.oicp.net:4321/Backup.aspx"
static inline double radians (double degrees) {return degrees * M_PI/180;}

@implementation ContactsViewController

int currentSelectedRow=-1;
BOOL dataSourceIsSrchResults=FALSE;

-(NSString *)generateContactsXML
{
	NSMutableString *strCont=[[NSMutableString alloc] initWithString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><Contacts>"];
	int i=0;
	for (i=0; i<[selectedRows count]; i++) {
		ABContact *cont=[ABContact contactWithRecordID:(ABRecordID)[[self.selectedRows objectAtIndex:i] integerValue]];
		strCont=[strCont stringByAppendingString:[cont ToXMLString]];
	}
	
	strCont=[strCont stringByAppendingString:@"</Contacts>"];
	
	return strCont;
}

#pragma mark -
#pragma mark Toolbar delegate
-(void)ViewBackupContacts:(id)sender{
	UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"share" message:@"Share clicked" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

-(void)Share:(id)sender
{
	UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"share" message:@"Share clicked" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

-(void)View:(id)sender
{
	[self viewCurrentContact];
}


-(void)Backup:(id)sender
{

	NSString *soapMsg=[[NSString alloc]initWithString: [self generateContactsXML]];
	NSURL *buURL=[NSURL URLWithString:Backup_Url];
	NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:buURL cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
	[serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
	[serviceRequest setHTTPMethod:@"POST"];
	[serviceRequest setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
	
	NSURLResponse * serviceResponse;
	NSError * serviceError;
	NSData *nd = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
	
	/* //Get device uniq number
	UIDevice *deviceNum=[[UIDevice alloc] init];
	UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"share" message:deviceNum.uniqueIdentifier delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
	[deviceNum release];
	[alert show];
	[alert release];
	*/
}

- (void) viewDidLoad
{
	self.title=@"Local Contacts";
	self.dataSourceArr = [ABContactsHelper contacts];
	[super viewDidLoad];
	//Add tabbar
	CustUIToolBar *toolBar=[[CustUIToolBar alloc] init];
	toolBar.tintColor=BAR_COLOR;
	[toolBar sizeToFit];
	[toolBar setDelegate:self];

	self.navigationItem.titleView = toolBar;
	[toolBar release];
	
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	[super viewDidUnload];
}

- (void)dealloc {
	[super dealloc];
}


@end
